<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | Xml v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace common;

class Xml {

    /**
     * 获取节点下面的子节点
     *
     * @param \DOMNode $node
     * @param $tagName
     * @return array
     */
    public static function getChildNodesByTagName(\DOMNode $node, $tagName) {
        $nodes = [];
        foreach ($node->childNodes as $childNode) {
            if ($childNode instanceof \DOMElement) {
                if ($childNode->tagName == $tagName) {
                    $nodes[] = $childNode;
                }
            }
        }
        return $nodes;
    }

    /**
     * 获取xml节点下面的某个子节点内容
     *
     * @param \DOMNode $node
     * @param $tagName
     * @return bool
     */
    public static function getXmlNodeValue(\DOMNode $node, $tagName, $defaultVal = false) {
        if (!$node) {
            return $defaultVal;
        }
        $tagNodes = $node->getElementsByTagName($tagName);
        if (!empty($tagNodes) && $tagNodes->item(0)) {
            return self::getNodeValue($tagNodes->item(0), $defaultVal);
        }
        return $defaultVal;
    }

    /**
     * 获取xml文档中的值列表
     *
     * @param $node
     * @param $tagName
     * @param $subTagName
     * @return array|bool
     */
    public static function getXmlNodeValueList(\DOMNode $node, $tagName, $subTagName) {
        if (!$node) {
            return [];
        }
        $tagNodes = $node->getElementsByTagName($tagName);
        if (empty($tagNodes) || !$tagNodes->item(0)) {
            return [];
        }
        $valueList = [];
        $subTagNodes = $tagNodes->item(0)->getElementsByTagName($subTagName);
        foreach ($subTagNodes as $subTagNode) {
            $valueList[] = self::getNodeValue($subTagNode);
        }
        return $valueList;
    }

    /**
     * 获取xml节点属性值
     *
     * @param \DOMNode $node
     * @param $attrName
     * @param $defaultVal
     * @return string
     */
    public static function getNodeAttrValue(\DOMNode $node, $attrName, $defaultVal = '') {
        $attrName = strtolower($attrName);
        foreach ($node->attributes as $attr) {
            $nodeName = strtolower($attr->nodeName);
            $nodeType = strtolower($attr->nodeType);
            if ($nodeType == 2 && $nodeName == $attrName) {
                unset($attrName, $nodeName, $nodeType);
                return self::getNodeValue($attr, $defaultVal);
            }
        }
        return $defaultVal;
    }

    /**
     * 获取属性列表
     *
     * @param \DOMNode $node
     * @param string $attrNamePrefix
     * @return array
     */
    public static function getNodeAttrValueList(\DOMNode $node, $attrNamePrefix = "") {
        $values = [];
        $attrNamePrefix = strtolower($attrNamePrefix);
        foreach ($node->attributes as $attr) {
            $nodeName = strtolower($attr->nodeName);
            $nodeType = strtolower($attr->nodeType);
            if ($nodeType == 2 && startsWith($nodeName, $attrNamePrefix)) {
                $attrName = substr($nodeName, strlen($attrNamePrefix));
                $values[$attrName] = self::getNodeValue($attr, '');
            }
        }
        return $values;
    }

    /**
     * 获取节点的内容
     *
     * @param $node
     * @param $defaultVal
     * @return string
     */
    public static function getNodeValue($node, $defaultVal = '') {
        if (!$node) {
            return $defaultVal;
        }
        $nodeValue = $node->nodeValue;
        return trim($nodeValue);
    }
}